package com.silences.service.Impl;

import com.silences.mapper.UserMapper;
import com.silences.pojo.User;
import com.silences.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service// 告诉springbean工厂， 这里有个UserService bean
public class UserServiceImpl implements UserService {
    @Resource//把UserMapper 注入进来
    private UserMapper userMapper;
    @Override
    public Map<String, Object> getUsers(String username, int pageIndex, int pageSize) {
        Map<String,Object> map = new HashMap<String, Object>();
       List<User> list= userMapper.getUsers(username, (pageIndex-1)*pageSize, pageSize);
       int count= userMapper.countUser(username);
       map.put("data",list);
       map.put("count",count);
       map.put("msg","查询成功");
       map.put("code","0000");
        return map;
    }

    @Override
    public Map<String, Object> getUser(int id) {

            Map<String,Object> map = new HashMap<String, Object>();
            User user= userMapper.getUser(id);
            map.put("data",user);
            map.put("msg","查询成功");
            map.put("code","0000");
            return map;
    }

    @Override
    public Map<String, Object> addUser(User user) {
        Map<String,Object> map = new HashMap<String, Object>();
        userMapper.addUser(user);
        map.put("msg","添加成功");
        map.put("code","0000");
        return map;
    }

    @Override
    public Map<String, Object> updateUser(User user) {
        Map<String,Object> map = new HashMap<String, Object>();
        userMapper.updateUser(user);
        map.put("msg","修改成功");
        map.put("code","0000");
        return map;
    }

    @Override
    public Map<String, Object> deleteUser(int id) {
        Map<String,Object> map = new HashMap<String, Object>();
        userMapper.deleteUser(id);
        map.put("msg","删除成功");
        map.put("code","0000");
        return map;
    }

    @Override
    public Map<String, Object> deleteUsers(String[] idArr) {
        Map<String,Object> map = new HashMap<String, Object>();
        for(int i=0; i< idArr.length; i++){
            userMapper.deleteUser(Integer.parseInt(idArr[i]));
        }
        map.put("msg","批量删除成功");
        map.put("code","0000");
        return map;
    }
}
